package com.zh.webflux.web;

import com.alibaba.fastjson2.JSON;
import com.zh.webflux.log.annotation.OperateLog;
import com.zh.webflux.util.RequestParamWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.io.Serializable;
import java.util.Map;


/**
 * Demo示例
 *
 * @author Zhou
 * @date 13:21 2022/12/20
 */
@Slf4j
@AllArgsConstructor
@Component
public class HelloHandler {

    @OperateLog("echo")
    public Mono<ServerResponse> echo(ServerRequest request) {
        String text = request.queryParam("text")
                .orElseThrow(() -> new IllegalArgumentException("参数不能未空"));

        Mono<Map<String, String>> helloMono = Mono.just(Map.of("text", text));
//        ping();
        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(helloMono, Map.class);
    }

    @OperateLog("params传参")
    public Mono<ServerResponse> params(ServerRequest request) {
        var form = RequestParamWrapper.wrapper(request.queryParams(), PersonForm.class);

        Mono<Map<String, String>> helloMono = Mono.just(Map.of("text", JSON.toJSONString(form)));

        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(helloMono, Map.class);
    }

    @OperateLog("form-data传参")
    public Mono<ServerResponse> formData(ServerRequest request) {

        Mono<Map<String, String>> helloMono = request.multipartData()
                .map(e -> RequestParamWrapper.wrapper(RequestParamWrapper.toMultiValueMap(e), PersonForm.class))
                .map(e -> Map.of("text", JSON.toJSONString(e)));

        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(helloMono, Map.class);
    }

    @OperateLog("form-urlencoded传参")
    public Mono<ServerResponse> formUrlencoded(ServerRequest request) {
        Mono<Map<String, String>> helloMono = request.formData()
                .map(e -> RequestParamWrapper.wrapper(e, PersonForm.class))
                .map(e -> Map.of("text", JSON.toJSONString(e)));

        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(helloMono, Map.class);
    }

    @OperateLog("bodyJson传参")
    public Mono<ServerResponse> bodyJson(ServerRequest request) {

        Mono<Map<String, String>> helloMono = request.bodyToMono(PersonForm.class)
                .map(e -> Map.of("text", JSON.toJSONString(e)));

        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(helloMono, Map.class);
    }


    @Data
    public static class PersonForm implements Serializable {
        private String name;
        private String age;
    }
}
